<?php
namespace app\admin\controller;

use app\admin\service\Category as CategoryService;
use think\facade\Request;
use think\facade\View;

class Category extends Controller
{
    public function index()
    {
        $type = input("type");
        if (Request::isAjax()) {
            $condition[] = ['type', "=", $type];
            $sex =  input("sex");
            if(!empty($sex)){
                $condition[] = ["sex", "=", $sex];
            }
            $keyword =  input("keyword");
            if(!empty($keyword)){
                $condition[] = ['name','like',"%".$keyword."%"];
            }
            $category = new CategoryService();
            $cateList = $category->getList($condition);
            return $this->success("获取成功", $cateList);
        }else {
            View::assign('type', $type);
            return View::fetch();
        }
    }

    public function add()
    {
        $type = input("type");
        $category = new CategoryService();
        if (Request::isAjax()) {
            $sex = input("sex", 1);//性别： 1：女 2：男
            $pid = input("pid", 0);//上级分类id
            $name = input("name");
            $sort = input("sort", 99);
            $status = input("status", 1);
            $data = [
                "sex" => $sex,
                "pid" => $pid,
                "name" => $name,
                "sort" => $sort,
                "status" => $status,
                "type" => $type,
            ];
            $res = $category->add($data);
            if (!$res) $this->error($category->getError());
            return $this->success('添加成功');
        }else {
            View::assign('type', $type);
            $cateList = $category->getList(["pid" => 0]);
            View::assign('cate_list', $cateList['list']);
            return View::fetch();
        }
    }

    public function edit()
    {
        $type = input("type");
        $category = new CategoryService();
        if (Request::isAjax()) {
            $post = input();
            $res = $category->edit($post, ["id" => $post['id'], "type" => $type]);
            if (!$res) $this->error($category->getError());
            return $this->success('修改成功');
        } else {
            View::assign('type', $type);

            $id = input('id');
            if (empty($id)) return $this->error('非法操作');
            $info = $category->detail($id);

            if (empty($info)) return $this->error('菜单不存在');
            View::assign('info', $info);

            $cateList = $category->getList(["pid" => 0]);
            View::assign('cate_list', $cateList['list']);
            return View::fetch('add');
        }
    }

    public function del()
    {
        $type = input("type");
        if (Request::isAjax()) {
            $ids = input('id');
            if (empty($ids)) return $this->error('请选择删除项');
            $category = new CategoryService();
            $res = $category->del($ids, $type);
            if (!$res) $this->error($category->getError());
            return $this->success('删除成功');
        }
    }

}
